package com.neulogistics.contrib.usercenter.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.contrib.usercenter.api.application.command.*;
import com.neulogistics.contrib.usercenter.api.application.query.IUserQueryService;
import com.neulogistics.contrib.usercenter.api.application.query.UserQuery;
import com.neulogistics.contrib.usercenter.api.application.viewmodel.LoginUserViewModel;
import com.neulogistics.contrib.usercenter.api.application.viewmodel.UserViewModel;
import com.neulogistics.contrib.usercenter.domain.model.aggregate.User;
import com.neulogistics.contrib.usercenter.domain.model.enumeration.UserStatus;
import com.neulogistics.contrib.usercenter.domain.repository.IUserRepository;
import com.neulogistics.infrastructure.common.query.PageViewModel;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@Api(tags = "用户管理")
@RequestMapping("/userCenter/user")
public class UserController {
    @Resource
    IUserRepository userRepository;
    @Resource
    IUserQueryService userQueryService;
    @Resource
    IMediator mediator;

    @ApiOperation("登录")
    @PostMapping("/login")
    public BaseResponse<LoginUserViewModel> login(@RequestBody @Validated @NotNull LoginCommand command){
        // 已校验数据格式
        BaseResponse<LoginUserViewModel> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("登出")
    @PostMapping("/logout")
    public BaseResponse login(@RequestBody @Validated @NotNull LogoutCommand command){
        // 已校验数据格式
        BaseResponse<String> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("创建用户")
    @PostMapping("/add")
    public BaseResponse<String> createUser(@RequestBody @Validated @NotNull CreateUserCommand command){
        // 已校验数据格式
        BaseResponse<String> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public BaseResponse<String> deleteUser(@RequestBody @NotNull UserIdCommand command){
        // 已校验数据格式
        userRepository.deleteById(command.userId);
        // 返回
        return ResultUtils.success("删除成功！");
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/edit")
    public BaseResponse<Boolean> updateUser(@RequestBody @NotNull UpdateUserCommand command){
        // 已校验数据格式
        BaseResponse<Boolean> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("修改用户状态")
    @PostMapping("/change")
    public BaseResponse changeUserStatus(@RequestBody @NotNull UserIdCommand command){
        // 已校验数据格式
        User user = userRepository.findById(command.userId);
        user.setUserStatus(user.getUserStatus()== UserStatus.Disable? UserStatus.Enable:UserStatus.Disable);
        userRepository.save(user);
        return ResultUtils.success(null);
    }

    @ApiOperation("重置用户密码")
    @PostMapping("/reset")
    public BaseResponse resetUserPassword(@RequestBody @NotNull UserIdCommand command){
        // 已校验数据格式
        User user = userRepository.findById(command.userId);
        user.setPassword("e10adc3949ba59abbe56e057f20f883e");
        userRepository.save(user);
        return ResultUtils.success(null);
    }

    @ApiOperation("修改用户密码")
    @PostMapping("/password")
    public BaseResponse changeUserPassword(@RequestBody @NotNull ChangePasswordCommand command){
        // 已校验数据格式
        BaseResponse<Boolean> baseResponse = mediator.send(command);
        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("根据id查询用户信息")
    @PostMapping("/get")
    public BaseResponse<UserViewModel> get(@RequestBody @Positive @NotNull UserIdCommand command){
        // 已校验数据格式
        // 查询
        UserViewModel userViewModel = userQueryService.getUserById(command.userId);
        // 修改成功，返回
        return ResultUtils.success(userViewModel);
    }

    @ApiOperation("根据条件分页查询用户信息")
    @PostMapping("/page")
    public BaseResponse<Page<UserViewModel>> getUserByPage(@RequestBody @Validated @NotNull UserQuery UserQuery){
        // 已校验数据格式
        // 查询
        Page<UserViewModel> page = userQueryService.getUserByPage(UserQuery);
        // 修改成功，返回
        return ResultUtils.success(page);
    }

}
